const hours = []
const minutes = []
for (let i = 0; i <= 23; i++) {
  if(i<10){
    var temp = "0" + i
  }else{
    var temp = "" + i
  }
  hours.push(temp)
}
for (let i = 0; i <= 59; i++) {
  if(i<10){
    var temp = "0" + i
  }else{
    var temp = "" + i
  }
  minutes.push(temp)
}


Page({
  data: {
    //setData:"",
    deviceId:"",
    uuid:"",
    characteristicId_0:"",
    characteristicId_1:"",
    isConnent:false,
    isIndex:true,
    isSetTime_1:false,
    isSetTime_2:false,
    isSetTime_3:false,

    isSetTimeOut:0,
    power:1,//开关
    isAddHot:0,
    isClear:0,
    isLock:0,
    setHot:50,
    nowHot:20,

    hours,
    minutes,
    value:[0,0,0],
    startHourTime_1:"00",
    startMinuteTime_1:"00",
    endHourTime_1:"00",
    endMinuteTime_1:"00",
    startHourTime_2:"00",
    startMinuteTime_2:"00",
    endHourTime_2:"00",
    endMinuteTime_2:"00",
    startHourTime_3:"00",
    startMinuteTime_3:"00",
    endHourTime_3:"00",
    endMinuteTime_3:"00",

    //定时任务页的初始数据
    startHour_1:"00",
    startMinute_1:"00",
    endHour_1:"00",
    endMinute_1:"00",
    startHour_2:"00",
    startMinute_2:"00",
    endHour_2:"00",
    endMinute_2:"00",
    startHour_3:"00",
    startMinute_3:"00",
    endHour_3:"00",
    endMinute_3:"00"
  },


  //事件处理函数:
  timeOutClick:function (){
    if(this.data.isLock || !this.data.power){
      return
    }
    this.setData({
      isIndex:false
    })
  },
  backIndexClick:function (){
    this.setData({
      isIndex:true
    })
  },
  goToSetTime_1:function(){
    this.setData({
      isSetTime_1:true
    })
  },
  backSetTimeOut_1:function (){
    this.setData({
      isSetTime_1:false
    })
  },
  goToSetTime_2:function(){
    this.setData({
      isSetTime_2:true
    })
  },
  backSetTimeOut_2:function (){
    this.setData({
      isSetTime_2:false
    })
  },
  goToSetTime_3:function(){
    this.setData({
      isSetTime_3:true
    })
  },
  backSetTimeOut_3:function (){
    this.setData({
      isSetTime_3:false
    })
  },



  finishClick_1:function(){
    var startTimes = parseInt(this.data.startHourTime_1) * 60 + parseInt(this.data.startMinuteTime_1) 
    var endTimes = parseInt(this.data.endHourTime_1) * 60 + parseInt(this.data.endMinuteTime_1)
    var val_start = "107" + "-" + startTimes
    var val_end = "108" + "-" +  endTimes
    if(((this.data.startHour_2 != "00") || (this.data.startMinute_2 != "00") || (this.data.endHour_2 != "00") || (this.data.endMinute_2 != "00")) && (endTimes >= (parseInt(this.data.startHour_2) * 60 + parseInt(this.data.startMinute_2)))){
      wx.showToast({
        title: '结束必须小于于时段二开始！',
      })
      return
    }
    if(startTimes < endTimes){
      this.setTimeOutFinish(val_start)
      this.setTimeOutFinish(val_end)
      wx.showToast({
        title: '设置成功！',
      })
    }else{
      wx.showToast({
        title: '结束应该大于开始！',
      })
    }   
  },
  finishClick_2:function(){
    var startTimes = parseInt(this.data.startHourTime_2) * 60 + parseInt(this.data.startMinuteTime_2) 
    var endTimes = parseInt(this.data.endHourTime_2) * 60 + parseInt(this.data.endMinuteTime_2)
    var val_start = "110" + "-" + startTimes
    var val_end = "111" + "-" +  endTimes
    if(this.data.startHour_1 == "00" && this.data.startMinute_1 == "00" && this.data.endHour_1 == "00" && this.data.endMinute_1 == "00"){
      wx.showToast({
        title: '先设置时段一',
      })
      return
    }
    if(((this.data.startHour_3 != "00") || (this.data.startMinute_3 != "00") || (this.data.endHour_3 != "00") || (this.data.endMinute_3 != "00")) && (endTimes >= (parseInt(this.data.startHour_3) * 60 + parseInt(this.data.startMinute_3)))){
      wx.showToast({
        title: '结束必须小于于时段三开始！',
      })
      return
    }
    if(startTimes <= (parseInt(this.data.endHour_1)*60 + parseInt(this.data.endMinute_1))){
      wx.showToast({
        title: '必须大于时段一结束时间！',
      })
      return
    }
    if(startTimes < endTimes){
      this.setTimeOutFinish(val_start)
      this.setTimeOutFinish(val_end)
      wx.showToast({
        title: '设置成功！',
      })
    }else{
      wx.showToast({
        title: '结束应该大于开始！',
      })
    }  
  },
  finishClick_3:function(){
    var startTimes = parseInt(this.data.startHourTime_3) * 60 + parseInt(this.data.startMinuteTime_3) 
    var endTimes = parseInt(this.data.endHourTime_3) * 60 + parseInt(this.data.endMinuteTime_3)
    var val_start = "113" + "-" + startTimes
    var val_end = "114" + "-" +  endTimes
    if(this.data.startHour_2 == "00" && this.data.startMinute_2 == "00" && this.data.endHour_2 == "00" && this.data.endMinute_2 == "00"){
      wx.showToast({
        title: '先设置时段二',
      })
      return
    }  
    if(startTimes <= (parseInt(this.data.endHour_2)*60 + parseInt(this.data.endMinute_2))){
      wx.showToast({
        title: '必须大于时段二结束时间！',
      })
      return
    }
    if(startTimes < endTimes){
      this.setTimeOutFinish(val_start)
      this.setTimeOutFinish(val_end)
      wx.showToast({
        title: '设置成功！',
      })
    }else{
      wx.showToast({
        title: '结束应该大于开始！',
      })
    }  
  },


  //定时开关按钮改变触发
  setTimeOutChange:function () {
    if(this.data.isSetTimeOut){
      var val = "118" + "-" + "0"
      this.writeBLECharacteristicValue(val)
    }else{
      var val = "118" + "-" + "1"
      this.writeBLECharacteristicValue(val)
    }
    
  },
  startChange_1:function(e){                   
    const val = e.detail.value
    this.setData({
      startHourTime_1:this.data.hours[val[0]],
      startMinuteTime_1:this.data.minutes[val[2]]
    })
  },
  endChange_1:function(e){
    const val = e.detail.value
    this.setData({
      endHourTime_1:this.data.hours[val[0]],
      endMinuteTime_1:this.data.minutes[val[2]]
    })
  },
  startChange_2:function(e){
    const val = e.detail.value
    this.setData({
      startHourTime_2:this.data.hours[val[0]],
      startMinuteTime_2:this.data.minutes[val[2]]
    })
  },
  endChange_2:function(e){
    const val = e.detail.value
    this.setData({
      endHourTime_2:this.data.hours[val[0]],
      endMinuteTime_2:this.data.minutes[val[2]]
    })
  },
  startChange_3:function(e){
    const val = e.detail.value
    this.setData({
      startHourTime_3:this.data.hours[val[0]],
      startMinuteTime_3:this.data.minutes[val[2]]
    })
  },
  endChange_3:function(e){
    const val = e.detail.value
    this.setData({
      endHourTime_3:this.data.hours[val[0]],
      endMinuteTime_3:this.data.minutes[val[2]]
    })
  },


  powerClick:function (){
    if(this.data.isLock){
      return
    }
    if(this.data.power == 0){
      var val = "101" + "-" + "1"
      this.writeBLECharacteristicValue(val)
    }else{
      var val = "101" + "-" + "0"
      this.writeBLECharacteristicValue(val)
    }
  },
  addHotClick:function(){
    if(!this.data.power || this.data.isLock){
      return
    }
    if(this.data.isAddHot == 0){
      var val = "104" + "-" + "1"
      this.writeBLECharacteristicValue(val)   
    }else{
      var val = "104" + "-" + "0"
      this.writeBLECharacteristicValue(val)
    }
  },
  clearClick:function(){
    if(!this.data.power || this.data.isLock){
      return
    }
    if(this.data.isClear == 0){
      var val = "105" + "-" + "1"
      this.writeBLECharacteristicValue(val)   
    }else{
      var val = "105" + "-" + "0"
      this.writeBLECharacteristicValue(val)
    }
  },
  lockClick:function(){
    if(!this.data.power){
      return
    }
    if(this.data.isLock == 0){
      var val = "106" + "-" + "1"
      this.writeBLECharacteristicValue(val)   
    }else{
      var val = "106" + "-" + "0"
      this.writeBLECharacteristicValue(val)
    }  
  },
  /*
  dataInput:function(e){
    this.setData({
      setData: e.detail.value
    })
  },
  sendData:function(){
    var val = this.data.setData
    this.writeBLECharacteristicValue(val)  
  },
 */
  //十六进制字符串转10进制方法
  hexInt:function(hex) {
    var len = hex.length, a = new Array(len), code;
    for (var i = 0; i < len; i++) {
        code = hex.charCodeAt(i);
        if (48<=code && code < 58) {
            code -= 48;
        } else {
            code = (code & 0xdf) - 65 + 10;
        }
        a[i] = code;
    } 
    return a.reduce(function(acc, c) {
        acc = 16 * acc + c;
        return acc;
    }, 0);
  },

  //滑动条完成一次滑动操作时执行
  sliderchange:function(event){
    var val = "103" + "-" + event.detail.value
    this.writeBLECharacteristicValue(val)
  },



  connent:function(){
    this.openBluetoothAdapter()
  },

  //初始化本机蓝牙模块
  openBluetoothAdapter:function(){
    wx.openBluetoothAdapter({
      success: (res) => {
        this.startBluetoothDevicesDiscovery()
      },
      fail: (res) => {
        wx.showToast({
          title: '手机蓝牙未打开!',
        })
      }
    })
  },

  //搜索附近的蓝牙设备
  startBluetoothDevicesDiscovery:function(){
    if(!this.data.connent){
      wx.showLoading({
        title: '连接中......',
      })
    }
    //this._discoveryStarted = true
    wx.startBluetoothDevicesDiscovery({
      success: (res) => {
        this.onBluetoothDeviceFound()
        
      },
    })  
  },

  //监听新的蓝牙设备的出现
  onBluetoothDeviceFound() {
    wx.onBluetoothDeviceFound((res) => {
      res.devices.forEach(device => {
        if (!device.name && !device.localName) {
          return
        }
        if(device.name == "YMIOT-BT"){
          //console.log(device)  //name:YMIOT-BT  mac:不同  
          this.setData({
            deviceId:device.deviceId
          })
          this.createBLEConnection()
        } 
      })
    })
  },
  
  //与目标蓝牙建立连接
  createBLEConnection:function(){
    const deviceId = this.data.deviceId
    //const deviceId = "0000FFF0-0000-1000-8000-00805F9B34FB"
    wx.createBLEConnection({
      deviceId,
      success: (res) => {
        //获取蓝牙设备所有服务 this.getBLEDeviceServices(id)
        this.getBLEDeviceServices()
      },
      fail:(res) => {
        //console.log(res)
        console.log("连接失败！")
      }
    })
    this.stopBluetoothDevicesDiscovery()
  },

  //停止搜索附近的蓝牙设备
  stopBluetoothDevicesDiscovery:function() {
    wx.stopBluetoothDevicesDiscovery()
  },
  
  //获取蓝牙设备所有服务
  getBLEDeviceServices:function(){
    const deviceId = this.data.deviceId
    wx.getBLEDeviceServices({
      // 这里的 deviceId 需要已经通过 createBLEConnection 与对应设备建立链接
      deviceId,
      success:(res) => {
        const uuid = res.services[1].uuid
        //console.log('device services:', res.services)
        //console.log(uuid)
        this.setData({
          uuid:uuid
        })
        this.getBLEDeviceCharacteristics()
      },
      fail:function(){
        console.log("获取服务失败！")
      }
    })
  },

  //获取蓝牙设备某个服务中所有特征值
  getBLEDeviceCharacteristics:function(){
    const deviceId = this.data.deviceId
    const serviceId = this.data.uuid
    wx.getBLEDeviceCharacteristics({
      // 这里的 deviceId 需要已经通过 createBLEConnection 与对应设备建立链接
      deviceId,
      // 这里的 serviceId 需要在 getBLEDeviceServices 接口中获取
      serviceId,
      success: (res) => {
        //console.log('device getBLEDeviceCharacteristics:', res.characteristics)
        this.setData({
          characteristicId_0:res.characteristics[0].uuid,
          characteristicId_1:res.characteristics[1].uuid
        })
        this.notifyBLECharacteristicValueChange()
      }
    })
  },

  //向低功耗蓝牙设备特征值中写入二进制数据
  writeBLECharacteristicValue:function(val){
    const arr = val.split("-")
    const val_0 = arr[0]
    const val_1 = arr[1]
    const deviceId = this.data.deviceId
    const serviceId = this.data.uuid
    const characteristicId = this.data.characteristicId_1
    //const characteristicId = "0000FFF2-0000-1000-8000-00805F9B34FB"

    let buffer = new ArrayBuffer(2)
    let dataView = new DataView(buffer)
    dataView.setUint8(0, parseInt(val_0))
    dataView.setUint8(1,parseInt(val_1))
    wx.writeBLECharacteristicValue({
      // 这里的 deviceId 需要在 getBluetoothDevices 或 onBluetoothDeviceFound 接口中获取
      deviceId,
      // 这里的 serviceId 需要在 getBLEDeviceServices 接口中获取
      serviceId,
      // 这里的 characteristicId 需要在 getBLEDeviceCharacteristics 接口中获取
      characteristicId,
      // 这里的value是ArrayBuffer类型
      value: buffer,
      success (res) {
        //console.log('writeBLECharacteristicValue success', res.errMsg)
      }
    })
  },

  //启用低功耗蓝牙设备特征值变化时的 notify 功能
  notifyBLECharacteristicValueChange:function(){
    const deviceId = this.data.deviceId
    const serviceId = this.data.uuid
    const characteristicId = this.data.characteristicId_0
    //const characteristicId = "0000FFF1-0000-1000-8000-00805F9B34FB"
    wx.notifyBLECharacteristicValueChange({
      state: true, // 启用 notify 功能
      // 这里的 deviceId 需要已经通过 createBLEConnection 与对应设备建立链接
      deviceId,
      // 这里的 serviceId 需要在 getBLEDeviceServices 接口中获取
      serviceId,
      // 这里的 characteristicId 需要在 getBLEDeviceCharacteristics 接口中获取
      characteristicId,
      success: (res) => {
        //console.log('notifyBLECharacteristicValueChange success', res.errMsg)
        this.onBLECharacteristicValueChange() 
        wx.hideLoading({
          complete: (res) => {
        
          },
        })
        this.setData({
          isConnent:true
        })
        wx.showToast({
          title: '蓝牙连接成功！',
        })
        this.onBLEConnectionStateChange()
      },
      fail:(res) => {
        console.log("notify功能开启失败！")
        wx.showToast({
          title: '连接失败！请打开蓝牙再试！',
        })
      }
    })
  },

  //监听低功耗蓝牙设备的特征值变化事件
  onBLECharacteristicValueChange:function(){
    // ArrayBuffer转16进制字符串示例
    function ab2hex(buffer) {
      let hexArr = Array.prototype.map.call(
      new Uint8Array(buffer),
      function(bit) {
        return ('00' + bit.toString(16)).slice(-2)
      }
      )
      return hexArr.join('');
    }
    wx.onBLECharacteristicValueChange((res) => {
      //console.log(`characteristic ${res.characteristicId} has changed, now is ${res.value}`)
      //console.log(ab2hex(res.value))
      if(ab2hex(res.value) == "ff"){
        return
      }
      var pushData = ab2hex(res.value)
      if(pushData.slice(0,2) == '65'){
        var setData = this.hexInt(pushData.slice(2,4))
        this.setData({
          power: setData 
        })
        return
      }
      if(pushData.slice(0,2) == '66'){
        var setData = this.hexInt(pushData.slice(2,4))
        this.setData({
          nowHot: setData 
        })
        return
      }
      if(pushData.slice(0,2) == '67'){
        var setData = this.hexInt(pushData.slice(2,4))
        this.setData({
          setHot: setData 
        })
        return
      }
      if(pushData.slice(0,2) == '68'){
        var isAdd = parseInt(pushData.slice(2,4))
        this.setData({
          isAddHot: isAdd 
        })
        return
      }
      if(pushData.slice(0,2) == '69'){
        var isAdd = parseInt(pushData.slice(2,4))
        this.setData({
          isClear: isAdd 
        })
        return
      }
      if(pushData.slice(0,2) == '6a'){
        var isAdd = parseInt(pushData.slice(2,4))
        this.setData({
          isLock: isAdd 
        })
        return
      }
      if(pushData.slice(0,2) == "6b"){
        if(pushData.length != 6){
          pushData = pushData.slice(0,2) + "00" + pushData.slice(2,4)
        }
        var tempString = pushData.slice(2,6)
        var str_0 = tempString.substr(3,1)
        str_0 = this.dulData(str_0)
        var str_1 = tempString.substr(2,1)
        str_1 = this.dulData(str_1)
        var str_2 = tempString.substr(1,1)
        str_2 = this.dulData(str_2)    
        var times = parseInt(str_0) + parseInt(str_1) * 16 + parseInt(str_2) * 16 * 16 
        var hours = Math.floor(times / 60)
        var minutes = times % 60 
        if(hours<10){
          var hours = "0" + hours
        }else{
          var hours = "" + hours
        }
        if(minutes<10){
          var minutes = "0" + minutes
        }else{
          var minutes = "" + minutes
        }
        this.setData({
          startHour_1:hours,
          startMinute_1:minutes
        })  
      }
      if(pushData.slice(0,2) == "6c"){
        if(pushData.length != 6){
          pushData = pushData.slice(0,2) + "00" + pushData.slice(2,4)
        }
        var tempString = pushData.slice(2,6)
        var str_0 = tempString.substr(3,1)
        str_0 = this.dulData(str_0)
        var str_1 = tempString.substr(2,1)
        str_1 = this.dulData(str_1)
        var str_2 = tempString.substr(1,1)
        str_2 = this.dulData(str_2)      
        var times = parseInt(str_0) + parseInt(str_1) * 16 + parseInt(str_2) * 16 * 16 
        var hours = Math.floor(times / 60)
        var minutes = times % 60 
        if(hours<10){
          var hours = "0" + hours
        }else{
          var hours = "" + hours
        }
        if(minutes<10){
          var minutes = "0" + minutes
        }else{
          var minutes = "" + minutes
        }
        this.setData({
          endHour_1:hours,
          endMinute_1:minutes
        })  
      }
      if(pushData.slice(0,2) == "6e"){
        if(pushData.length != 6){
          pushData = pushData.slice(0,2) + "00" + pushData.slice(2,4)
        }
        var tempString = pushData.slice(2,6)
        var str_0 = tempString.substr(3,1)
        str_0 = this.dulData(str_0)
        var str_1 = tempString.substr(2,1)
        str_1 = this.dulData(str_1)
        var str_2 = tempString.substr(1,1)
        str_2 = this.dulData(str_2)    
        var times = parseInt(str_0) + parseInt(str_1) * 16 + parseInt(str_2) * 16 * 16 
        var hours = Math.floor(times / 60)
        var minutes = times % 60 
        if(hours<10){
          var hours = "0" + hours
        }else{
          var hours = "" + hours
        }
        if(minutes<10){
          var minutes = "0" + minutes
        }else{
          var minutes = "" + minutes
        }
        this.setData({
          startHour_2:hours,
          startMinute_2:minutes
        })  
      }
      if(pushData.slice(0,2) == "6f"){
        if(pushData.length != 6){
          pushData = pushData.slice(0,2) + "00" + pushData.slice(2,4)
        }
        var tempString = pushData.slice(2,6)
        var str_0 = tempString.substr(3,1)
        str_0 = this.dulData(str_0)
        var str_1 = tempString.substr(2,1)
        str_1 = this.dulData(str_1)
        var str_2 = tempString.substr(1,1)
        str_2 = this.dulData(str_2)    
        var times = parseInt(str_0) + parseInt(str_1) * 16 + parseInt(str_2) * 16 * 16 
        var hours = Math.floor(times / 60)
        var minutes = times % 60 
        if(hours<10){
          var hours = "0" + hours
        }else{
          var hours = "" + hours
        }
        if(minutes<10){
          var minutes = "0" + minutes
        }else{
          var minutes = "" + minutes
        }
        this.setData({
          endHour_2:hours,
          endMinute_2:minutes
        })  
      }
      if(pushData.slice(0,2) == "71"){
        if(pushData.length != 6){
          pushData = pushData.slice(0,2) + "00" + pushData.slice(2,4)
        }
        var tempString = pushData.slice(2,6)
        var str_0 = tempString.substr(3,1)
        str_0 = this.dulData(str_0)
        var str_1 = tempString.substr(2,1)
        str_1 = this.dulData(str_1)
        var str_2 = tempString.substr(1,1)
        str_2 = this.dulData(str_2)    
        var times = parseInt(str_0) + parseInt(str_1) * 16 + parseInt(str_2) * 16 * 16 
        var hours = Math.floor(times / 60)
        var minutes = times % 60 
        if(hours<10){
          var hours = "0" + hours
        }else{
          var hours = "" + hours
        }
        if(minutes<10){
          var minutes = "0" + minutes
        }else{
          var minutes = "" + minutes
        }
        this.setData({
          startHour_3:hours,
          startMinute_3:minutes
        })  
      }
      if(pushData.slice(0,2) == "72"){
        if(pushData.length != 6){
          pushData = pushData.slice(0,2) + "00" + pushData.slice(2,4)
        }
        var tempString = pushData.slice(2,6)
        var str_0 = tempString.substr(3,1)
        str_0 = this.dulData(str_0)
        var str_1 = tempString.substr(2,1)
        str_1 = this.dulData(str_1)
        var str_2 = tempString.substr(1,1)
        str_2 = this.dulData(str_2)    
        var times = parseInt(str_0) + parseInt(str_1) * 16 + parseInt(str_2) * 16 * 16 
        var hours = Math.floor(times / 60)
        var minutes = times % 60 
        if(hours<10){
          var hours = "0" + hours
        }else{
          var hours = "" + hours
        }
        if(minutes<10){
          var minutes = "0" + minutes
        }else{
          var minutes = "" + minutes
        }
        this.setData({
          endHour_3:hours,
          endMinute_3:minutes
        })  
      }
      if(pushData.slice(0,2) == "76"){
        var isAdd = parseInt(pushData.slice(2,4))
        this.setData({
          isSetTimeOut: isAdd 
        })
        return   
      }
    })
  },

  //监听低功耗蓝牙连接状态的改变事件
  onBLEConnectionStateChange:function(){
    wx.onBLEConnectionStateChange((res) => {
      // 该方法回调中可以用于处理连接意外断开等异常情况
     // console.log(`device ${res.deviceId} state has changed, connected: ${res.connected}`)
      if(!res.connected){
        this.setData({
          isConnent:false
        })
      }else{
        console.log("蓝牙已连接")
      }
    })
  },



  //特别针对于定时设置的方法
  setTimeOutFinish:function(val){
    const arr = val.split("-")
    const val_0 = arr[0]
    const val_1 = arr[1]
    const deviceId = this.data.deviceId
    const serviceId = this.data.uuid
    const characteristicId = this.data.characteristicId_1
    //const characteristicId = "0000FFF2-0000-1000-8000-00805F9B34FB"

    let buffer = new ArrayBuffer(3)
    let dataView = new DataView(buffer)
    dataView.setUint8(0, parseInt(val_0))
    dataView.setUint16(1,parseInt(val_1))
    wx.writeBLECharacteristicValue({
      // 这里的 deviceId 需要在 getBluetoothDevices 或 onBluetoothDeviceFound 接口中获取
      deviceId,
      // 这里的 serviceId 需要在 getBLEDeviceServices 接口中获取
      serviceId,
      // 这里的 characteristicId 需要在 getBLEDeviceCharacteristics 接口中获取
      characteristicId,
      // 这里的value是ArrayBuffer类型
      value: buffer,
      success (res) {
        //console.log('writeBLECharacteristicValue success', res.errMsg)
      }
   })
  },

  //针对十六进制的字母处理：
  dulData:function(val){
    if(val == "a" || val == "A"){
      return "10"
    }
    else if(val == "b" || val == "B"){
      return "11"
    }
    else if(val == "c" || val == "C"){
      return "12"
    }
    else if(val == "d" || val == "D"){
      return "13"
    }
    else if(val == "e" || val == "E"){
      return "14"
    }
    else if(val == "f" || val == "F"){
      return "15"
    }
    else{
      return val
    }
  }
})